﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCMS
{
    public class GeneralUtility
    {
        public static string DateToString(string aDate)
        {
            if (aDate == null || aDate.Length == 0)
            {
                // have to throw or display message
            }

            try
            {
                try
                {
                    if (aDate.Substring(0, 2).Equals("00"))
                    {
                        return aDate.Substring(6, 4) + "0000";
                    }

                    DateTime l_datetime = DateTime.Parse(aDate);
                    return l_datetime.Year.ToString("0000") + l_datetime.Month.ToString("00") + l_datetime.Day.ToString("00");
                }
                catch (FormatException ex)
                {
                    throw ex;
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                catch (ArgumentException ex)
                {
                    throw ex;
                }
                catch (Exception)
                {
                    return aDate.Substring(6, 4) + aDate.Substring(3, 2) + aDate.Substring(0, 2);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static string StringToDate(string aString)
        {
            if (aString == null || aString.Length == 0 || aString.Length < 8)
            {
                throw new NotImplementedException();
            }

            try
            {
                if (aString.Substring(4, 4).Equals("0000"))
                {
                    return aString.Substring(6, 2) + "/" + aString.Substring(4, 2) + "/" + aString.Substring(0, 4);
                }
                else
                {
                    return new DateTime(int.Parse(aString.Substring(0, 4)), int.Parse(aString.Substring(4, 2)), int.Parse(aString.Substring(6, 2))).ToShortDateString();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Int64 getSysKey()
        {
            DateTime dt = DateTime.Now;

            StringBuilder str = new StringBuilder();

            str.Append(dt.Year.ToString("00").Substring(0, 4));
            str.Append(dt.Month.ToString("00"));
            str.Append(dt.Day.ToString("00"));
            str.Append(dt.Hour.ToString("00"));
            str.Append(dt.Minute.ToString("00"));
            str.Append(dt.Second.ToString("00"));

            return Convert.ToInt64(str.ToString());
        }

        public static Int64 GenerateId()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }
    }
}
